/*
 *
 *方法的整理与重构
 *
 *
 */

//定义app参数名称，并加载其它模块
var authApp=angular.module('authapp', ["ngCookies",'ngFileUpload','ui.bootstrap']);

//此为定位页面所用
authApp.value('page','qrcodeResult');

/*controller部分*/


//定义父controller，主要用于controller之间传递信息
authApp.controller("fcontroller",function($scope,$http){
    //
    //****与分页相关的****
    //

    //从接口中获取总的分页数量
    $scope.$on('$getPageCountFromApi',function(event,data){
        $scope.$broadcast('$getPageCount',data);
    });

    //捕获分页点击事件并传递currentPage信息到主controller
    $scope.$on('$pageAction',function(event,data){
        $scope.$broadcast('$pageChanged',data);
    });
})


//主要操作controller
authApp.controller("allInstitutionList",function($scope,$http, $cookies){
    //针对此页面需要的参数 should edit
    $scope.isAddM=true;
    $scope.isAddPwd=true;
    $scope.isclickBtn=false;
    $scope.isSel01=true;
    $scope.isSel02=true;
    $scope.isSel03=true;
    $scope.accounttype = $cookies.get('accounttype')
    //**********主体方法
    $scope.ifFirstTimeQuery=true;
    $scope.pageNumber=0;
    $scope.pageSize=10;
    $scope.imgs=[]
    var queryMainApi=apis.querySchoolGroupMap;//！！！should edit
    $scope.queryMain=function(){
        $http({
            method:'get',
            url:queryMainApi,
            params:{},
        }).success(function(response){
            if(response.code=="000000"){
                $scope.queryMainApiCallBack(response);
                //将返回的pagecount和current传到上面的controller
                $scope.$emit('$getPageCountFromApi',response.data);
            }else{
            	console.log("aaaa")
                alert(response.errorMsg);
            }
        }).error(function(){
            alert("网络出现故障，请确认网络是否连接");
        })
    }
    //捕捉分页点击事件并做处理
    $scope.$on("$pageChanged",function(event,data){
        if(!$scope.ifFirstTimeQuery){//解决第一次会调用两次接口的问题
            $scope.pageNumber=data.currentPage-1;//这边之所以-1是因为后台接口第一页为0
            $scope.queryMain();
        }
        $scope.ifFirstTimeQuery=false;//改变是否是第一次调用接口的状态
    })

    $scope.queryMain();

    /*下面为自已编辑部分*/
    $scope.print = function(path) {
        return window.open('/views/investiga/table14.html?path=' + path) 
    }
    //调用主接口成功之后的处理方法
   $scope.queryMainApiCallBack=function(response){
       $scope.list=response.data;
       $scope.imgs = new Array($scope.list.length)
       if($scope.list.length != 0){
           $scope.totalCount = response.data.recordCount;
           $scope.pageSize = response.data.pageSize;
       }
   }

    //按钮添加事件
    $scope.addM=function(){
    	$scope.querySchoolByExpert()
        $scope.isAddM=false;
        $scope.isclickBtn=false;
    }

    //按钮取消事件
    $scope.cancelAddM=function(){
        $scope.isAddM=true;
        $scope.account="";
        $scope.schoolName="";
        $scope.major="";
    }
    $scope.shows=function(schoolId, index) {
        console.log(schoolId)
        $http({
            method:'get',
            url:apis.getTbCodeUrl,
            params:{'schoolId': schoolId},
        }).success(function(response){
            if(response.code=="000000"){
              $scope.imgs[index] = response.data.tb_code_url
            }else{
                alert(response.errorMsg);
            }
        }).error(function(){
            alert("服务器打瞌睡了，请稍等！");
            $scope.isAddM=true;
        })
    }
    //添加机构信息
    $scope.addFirstZ=function(){
        if(window.confirm("是否确定添加？")){

            var rJson={'schoolId':$scope.selected.p_id,'sign':$scope.sign_a};
            $http({
                method:'post',
                url:apis.addCRMap,
                data:rJson,
            }).success(function(response){
                if(response.code=="000000"){
                    console.log(response)
					$scope.isAddM=true;
					$scope.queryMain();
                }else{
                    alert(response.errorMsg);
                }
            }).error(function(){
                alert("服务器打瞌睡了，请稍等！");
                $scope.isAddM=true;
            })
        }
    }

    
//  $http({
//      method:'post',
//      url:apis.queryOneCR,
//      data:{'id':5},
//  }).success(function(response){
//      if(response.code=="000000"){
//          console.log(response)
//      }else{
//      	console.log("aaa")
//          alert(response.errorMsg);
//      }
//  }).error(function(){
//      alert("服务器打瞌睡了，请稍等！");
//      $scope.isAddPwd=true;
//  })

    //根据id查询机构信息
    $scope.queryAuthSchoolInfoById=function(pid,name,telephone,mobilePhone,email,address,postcode,remark){
        $scope.isAddM=false;
        $scope.isclickBtn=true;
        $scope.Id=pid;

        $scope.name=name;
        $scope.mobilePhone=telephone;
        $scope.telephone=mobilePhone;
        $scope.email=email;
        $scope.address=address;
        $scope.postcode=postcode;
        $scope.remark=remark;

    }

    //修改机构信息
    $scope.updateAuthSchoolInfoAccount=function(){
        if(window.confirm("是否确定修改？")){
            var rJson={'pid':$scope.Id,'name':$scope.name,'type':"1",'mobilePhone':$scope.mobilePhone,'telephone':$scope.telephone,
                'email':$scope.email,'address':$scope.address,'postcode':$scope.postcode,'remark':$scope.remark};

            $http({
                method:'post',
                url:apis.updateSchoolContacts,
                data:rJson,
            }).success(function(response){
                if(response.code=="000000"){
                    $scope.isAddM=true;
                    $scope.queryMain();
                    $scope.name="";
                    $scope.mobilePhone="";
                    $scope.telephone="";
                    $scope.email="";
                    $scope.address="";
                    $scope.postcode="";
                    $scope.remark="";
                }else{
                    alert(response.errorMsg);
                }
            }).error(function(){
                alert("服务器打瞌睡了，请稍等！");
                $scope.isAddM=true;
            })
        }
    }

    //删除机构信息
//  $scope.delCollect=function(pid){
//      if(window.confirm("是否确定删除？")){
//          var rJson={'pid':pid};
//          $http({
//              method:'post',
//              url:apis.deleteSchoolContacts,
//              data:rJson,
//          }).success(function(response){
//              if(response.code=="000000"){
//                  $scope.queryMain();
//              }else{
//                  alert(response.errorMsg);
//              }
//          })
//      }
//  }

	$scope.turnToCheck= function(aa, bb, cc, dd){
        if (!cc) return location.href =  paths.path.seeInformation+"?"+aa+"+"+bb;
		location.href = paths.path.seeInformation+"?"+aa+"+"+bb + '+' + encodeURIComponent(encodeURIComponent(cc)) + '+' + dd; 
	}
	$scope.turnToRevise = function(aa,bb,cc,dd){
		var str = encodeURIComponent(encodeURIComponent(cc));

		location.href = paths.path.table1+"?"+aa+"+"+bb+"+"+str+"+"+dd;
		
	}
	$scope.download = function(sign,schoolId) {
		location.href = apis.downCourseRecord+"?sign="+sign+"&schoolId="+schoolId;
	}
    //查询所有学校
		$scope.querySchoolByExpert=function(){
			$http({
		        method:'post',
		        url:apis.querySchoolByExpert,
		        data:{},
		    }).success(function(response){
		        if(response.code=="000000"){
		            console.log(response)
		            $scope.selectData = response.data;
		        }else{
		            alert(response.errorMsg);
		        }
		    }).error(function(){
		        alert("服务器打瞌睡了，请稍等！");
		        $scope.isAddPwd=true;
		    })
		}

})
		

//分页controller
authApp.controller('pageDemo',function($scope){
    //定义分页的初始参数（不改动，不会影响功能）
    $scope.currentPage=1;
    $scope.itemsPerPage=10;
    $scope.maxSize = 8;
    //需要接口返回的一些参数，如pagecount
    $scope.$on('$getPageCount',function(event,data){
        $scope.totalItems = parseInt(data.recordCount);
        $scope.bigTotalItems =parseInt(data.recordCount);
    });
    //分页点击的触发事件
    $scope.$watch('currentPage', function() {
        var pagedata={currentPage:$scope.currentPage};
        $scope.$emit('$pageAction',pagedata);
    });
})